/**
 * 
 */
package com.epower.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.epower.bo.epower.commons.Constants;
import com.epower.dataobject.EpowerSentenceDO;

/**
 * @author bolin.hbc
 */
public class HttpUtils {

    private static final Log log = LogFactory.getLog(HttpUtils.class);

    public static String getResponse(String url) {
        return getResponse(url, "GBK");
    }

    public static String getResponse(String url, String code) {

        try {
            URL getUrl = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) getUrl.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty("content-type", "text/html");
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.connect();

            InputStream response = httpURLConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(response, code));
            String line = null;
            StringBuilder content = new StringBuilder();
            while ((line = br.readLine()) != null) {
                content.append(line).append("\n");
            }

            return content.toString();
        } catch (Exception e) {
            log.error("http error!", e);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public static List<EpowerSentenceDO> getSentenceListBySpelling(String spelling) {

        List<EpowerSentenceDO> sList = new ArrayList<EpowerSentenceDO>();
        String urlSens = HttpUtils.getResponse(Constants.DICT_CN_SEARCH_WORD + spelling.toLowerCase());

        if (urlSens == null || urlSens.trim().isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        String lines[] = urlSens.split("\n");
        for (int i = lines.length - 2; i >= 0; i--) {
            String line = lines[i];

            if (line.indexOf(Constants.DICT_CN_SEARCH_DEF) == -1 && line.indexOf(Constants.DICT_CN_SEARCH_DEF) == -1) {

                String s[] = line.split("<trans>");
                if (s.length == 2) {
                    s[0] = s[0].replaceAll("<sent>", "").replaceAll("</orig>", "").replaceAll("<orig>", "").replaceAll("&lt;em", "<span class=\"b\"").replaceAll("em&gt;", "span>")
                            .replaceAll("&gt;", ">").replaceAll("&lt;", "<");
                    s[1] = s[1].replaceAll("<trans>", "").replaceAll("</trans>", "").replaceAll("</sent>", "").replaceAll("&lt;em", "<span class=\"b\"").replaceAll("em&gt;", "span>")
                            .replaceAll("&gt;", ">").replaceAll("&lt;", "<");
                    EpowerSentenceDO sentence = new EpowerSentenceDO();
                    sentence.setSentence(s[0]);
                    sentence.setExplains(s[1]);
                    sentence.setSpelling(spelling);
                    sentence.setStatus(Constants.STATUS_OK);
                    sList.add(sentence);
                }
            }

        }
        return sList;
    }

    public static void main(String[] args) {
        List<EpowerSentenceDO> sList = getSentenceListBySpelling("home");
        for (EpowerSentenceDO s : sList) {
            System.out.println(s.getSentence());
            System.out.println(s.getExplains());
        }
    }
}
